package com.Shop.Center.service;

import com.Shop.Center.entity.Items;
import com.Shop.Center.dao.ItemsMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ItemsService {
    @Autowired
    private ItemsMapper itemsMapper;

    public List<Items> getAllItems(){
        List<Items> items;
        items=itemsMapper.selectList(null);
        return items;
    }

    public Items getItemById(Long id){
        return itemsMapper.selectById(id);
    }

    @Transactional
    public int addItem(String name, String info, Double originPrice,
                       Double sellingPrice, Integer amount, char sellStatus,
                       String imageUrl, String type, String tag){
        return itemsMapper.insertItem(name,info,originPrice,sellingPrice,amount,sellStatus,imageUrl,type,tag);
    }

    @Transactional
    public int updateInfo(Items item){//会更新除id外的所有字段
        /*Items originItem = getItemById(item.getId());
        originItem.setAmount(item.getAmount());
        originItem.setName(item.getName());
        originItem.setInfo(item.getInfo());
        originItem.setOriginPrice(item.getOriginPrice());
        originItem.setSellingPrice(item.getSellingPrice());
        originItem.setSellStatus(item.getSellStatus());
        originItem.setImageUrl(item.getImageUrl());
        originItem.setType(item.getType());
        originItem.setTag(item.getTag());*/
        return itemsMapper.updateById(item);
    }

    public int updateSellStatus(Long id, char sellStatus) {
        Items item = getItemById(id);
        item.setSellStatus(sellStatus);
        return itemsMapper.updateById(item);
    }

    public int deleteItem(Long id) {
        return itemsMapper.deleteById(id);
    }

    public List<Items> getTypeItems(String type){
        List<Items> items;
        QueryWrapper<Items> queryWrapper=new QueryWrapper<>();
        if(type.equals("all")){
            queryWrapper=null;
        }else{
            queryWrapper.eq("type",type);
        }
        items=itemsMapper.selectList(queryWrapper);
        return items;
    }

    public List get(String type){
        QueryWrapper<Items> queryWrapper=new QueryWrapper<>();
        if(type.equals("all")){
            queryWrapper=null;
        }else{
            queryWrapper.eq("type",type);
        }
        return itemsMapper.selectList(queryWrapper);
    }
    public List search(String name){
        QueryWrapper<Items> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return itemsMapper.selectList(queryWrapper);
    }
}
